public class test10 {
    class Solution {
        public int candy(int[] ratings) {
            int[] sugar = new int[ratings.length];
            sugar[0] = 1;
            //左-右
            for(int i = 1;i<ratings.length;i++){
                if(ratings[i]>ratings[i-1]){
                    sugar[i] = sugar[i-1]+1;
                }else{
                    sugar[i] = 1;
                }
            }
            //右-左
            for(int i = ratings.length-1;i>0;i--){
                if(ratings[i-1]>ratings[i] && sugar[i-1]<=sugar[i]){
                    sugar[i-1] = sugar[i]+1;
                }
            }

            int res = 0;
            for(int s:sugar){
                res += s;
            }
            return res;
        }
    }
}
